﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using System.Web;
using Slam.FieldTypes;
using Microsoft.SharePoint.Utilities;

namespace SlamCms.SharePoint.FieldTypes
{
    public partial class FormRedirectFieldControl : SlamFieldControl
    {
        protected Label lblFieldValue;

        public override object Value
        {
            get
            {
                this.EnsureChildControls();

                return this.FieldValue;
            }
        }
        

        protected override string DefaultTemplateName
        {
            get
            {
                return "SlamCMSFormRedirectFieldControl";
            }
        }

        protected override void CreateChildControls()
        {
            if (Field == null)
                return;

            base.CreateChildControls();

            if (ControlMode == SPControlMode.Display)
                return;

            if (!base.IsFieldValueCached)
            {
                lblFieldValue = (Label)TemplateContainer.FindControl("lblFieldValue");
                lblFieldValue.Text = this.FieldValue;
            }
        }

        private string fieldValue;

        public string FieldValue
        {
            get { return fieldValue; }
            set { fieldValue = value; }
        }


        protected override void OnLoad(EventArgs e)
        {
            this.Parent.Parent.Visible = true;
            base.OnLoad(e);
            this.Parent.Parent.Visible = false;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (SPContext.Current.FormContext.FormMode == SPControlMode.Display)
            {

                SPListItem item = SPContext.Current.Item as SPListItem; 

                string contentType = HttpContext.Current.Request.QueryString["ContentTypeId"];
                string isDialog = HttpContext.Current.Request.QueryString["IsDlg"];
                string source = HttpContext.Current.Request.QueryString["Source"];

                //string url = String.Concat(item.Web.Url, "/", item.ParentList.Forms[PAGETYPE.PAGE_EDITFORM].Url, "?id=", item.ID.ToString(), (!String.IsNullOrEmpty      (contentType)?"&ContentTypeId="+contentType:String.Empty),(!String.IsNullOrEmpty(source)?"&Source="+source:String.Empty), "&IsDlg=", isDialog);

                string url = String.Concat(item.Web.Url, "/", item.ParentList.Forms[PAGETYPE.PAGE_EDITFORM].Url, "?id=", item.ID.ToString(), (!String.IsNullOrEmpty(contentType) ? "&ContentTypeId=" + contentType : String.Empty), !String.IsNullOrEmpty(isDialog) ? "&IsDlg="+isDialog:String.Empty, !String.IsNullOrEmpty(source)?"&Source="+SPHttpUtility.UrlKeyValueEncode(source):String.Empty);
                HttpContext.Current.Response.Redirect(url);
            }
        }

    }

}
